<?php
namespace App\Http\Controllers\Api;

use App\Models\MShopGood;
use App\Models\MShopOrders;
use App\Models\MUser;
use App\Models\MUsersCommission;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use EasyWeChat\Factory;


class DetailController extends ApiController{
    /**
     * 订单记录
     *
     */
    public function order_list(Request $request){

        // 验证用户信息
        $user_info = MUser::where(['account'=> $request->account])->first();
        if(empty($user_info))
        {
            return $this->failed('用户信息不存在', 403);
        }
        $page = $request->page??1;
        $size = $request->size??10;
        $start_count = ($page-1)*$size;

        $where = array();
        $where['uid'] = $user_info->id;

        $this->data['total'] = MShopOrders::where($where)->count();
        $order_list = MShopOrders::where($where)->select('id','goods_id','uid','order_sn','num','rmb_price','created_at','status','is_normal','cancel_reason','type')->offset($start_count)->limit($size)->get();

        if($order_list)
        {
            foreach ($order_list as $key =>$val)
            {
                $goods_info=MShopGood::where('id',$val['goods_id'])->first();
                $order_list[$key]['goods_name']=$goods_info->name;
            }
        }
        $this->data['order_list'] = $order_list;
        return $this->ReturnMessage(200,getMessage(),$this->data);
    }

    /**
     * 收益明细
     *
     */
    public function earn_list(Request $request){

        // 验证用户信息
        $user_info = MUser::where(['account'=> $request->account])->first();
        if(empty($user_info))
        {
            return $this->failed('用户信息不存在', 403);
        }

        $page = $request->page??1;
        $size = $request->size??10;
        $start_count = ($page-1)*$size;

        $where = array();
        $where['user_id'] = $user_info->id;

        $this->data['total'] = MUsersCommission::where($where)->count();
        $earn_list = MUsersCommission::where($where)->select('*')->offset($start_count)->limit($size)->get();
        if($earn_list)
        {
            foreach ($earn_list as $key =>$val)
            {
                $goods_info=MShopGood::where('id',$val['goods_id'])->first();
                $earn_list[$key]['goods_name']=$goods_info->name;
                $buy_user_info=MUser::where('id',$val['buy_user_id'])->first();
                $earn_list[$key]['buy_user_name']=$buy_user_info->nickname;
            }
        }
        $this->data['earn_list'] = $earn_list;
        return $this->ReturnMessage(200,getMessage(),$this->data);
    }

    /**
     * 推广记录
     *
     */
    public function recommend_list(Request $request){
        // 验证用户信息
        $user_info = MUser::where(['account'=> $request->account])->first();
        if(empty($user_info))
        {
            return $this->failed('用户信息不存在', 403);
        }

        $Shop_orders   = new MShopOrders();

        $page = $request->page??1;
        $size = $request->size??10;
        $start_count = ($page-1)*$size;
        $this->data['total']   = $Shop_orders
                         ->leftJoin('users as u', 'u.id', '=', 'shop_orders.uid')
                         ->leftJoin('user_relation as r', 'r.user_id', '=', 'u.id')
                         ->where('r.pid',$user_info->id)
                         ->where('r.lay','<=',2)->count();//查询满足要求的总记录数
        $order_list = $Shop_orders
                     ->leftJoin('users as u', 'u.id', '=', 'shop_orders.uid')
                     ->leftJoin('user_relation as r', 'r.user_id', '=', 'u.id')
                     ->where('r.pid',$user_info->id)
                     ->where('r.lay','<=',2)
                     ->select('shop_orders.id','shop_orders.goods_id','shop_orders.uid','shop_orders.order_sn','shop_orders.num','shop_orders.rmb_price','shop_orders.created_at','shop_orders.status','shop_orders.is_normal','shop_orders.cancel_reason','shop_orders.type','u.nickname','r.lay')
                     ->offset($start_count)->limit($size)->get();
        if($order_list)
        {
            foreach ($order_list as $key =>$val)
            {
                $goods_info=MShopGood::where('id',$val['goods_id'])->first();
                $order_list[$key]['goods_name']=$goods_info->name;
            }
        }
        $this->data['order_list'] = $order_list;
        return $this->ReturnMessage(200,getMessage(),$this->data);
    }

}
